package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartInfoService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/cart")
public class CartApiController {

    @Resource
    private CartInfoService cartInfoService;

    //加入购物车
    @PostMapping("/addCart/{skuId}/{skuNum}")
    public CartInfo addCart(@PathVariable Long skuId,@PathVariable Integer skuNum){
        return cartInfoService.addCart(skuId,skuNum);
    }

    //重定向到添加购物车
    @GetMapping("toCart/{skuId}")
    public CartInfo toCart(@PathVariable Long skuId){
        return cartInfoService.toCart(skuId);
    }

    //去购物车结算
    @GetMapping("/cartList")
    public Result cartList(HttpServletRequest request){
        //真实用户id    AuthContextHolder自己定义的工具类
        String userId = AuthContextHolder.getUserId(request);
        //临时用户id
        String userTempId = AuthContextHolder.getUserTempId(request);
        return Result.ok(cartInfoService.cartList(userId,userTempId));
    }

    //复选框
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId,@PathVariable Integer isChecked){
        cartInfoService.checkCart(skuId,isChecked);
        return Result.ok();
    }

    //查询选中的商品清单
    @GetMapping("/getCartListCecked")
    public List<CartInfo> getCartListCecked(HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        return cartInfoService.getCartListCecked(userId);
    }

}
